<?php
/**
 * WpAbstractV.php
 * 
 * This file contains the WpAbstractV class, an abstract class providing basic functionality for all classes 
 * based on SimpleDatexV.
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage dataaccess.wpdatex
 */

/**
 * An abstract helper class for SimpleDatexV
 * 
 * The WpAbstractV class is an abstract class providing basic functionality for all classes 
 * based on SimpleDatexV. It expects the class property $_arrJoin to be set by child classes in the following format: 
 * 
 * <code>
 * class MyData extends WpAbstractV
 * {
 * 		protected $_arrJoin = array(
 * 			array(
 * 				'table' => 'Table1',
 * 				'alias' => 'tab1',
 * 				'fields' => array(
 * 					array(
 * 						'name' => 'fieldname',
 * 						'alias' => 'fieldalias',
 * 						'type' => 'string'
 * 					),
 * 					array(...),
 * 					..
 * 				) 				
 * 			),
 * 			array(
 * 				'table' => 'Table2',
 * 				'alias' => 'tab2',
 * 				'fields' => array(),
 * 				'joinoperator' => '='
 * 				'jointype' => 'INNER JOIN',
 * 				'joinkey' => 'myid',
 * 				'jointarget' => 'tab1',
 * 				'jointargetkey' => 'tab2id',
 * 				'joinaddcondition' => '',
 * 			),
 * 			...
 * 		)
 * 
 * 		//Other functions to implement:
 * 		public function getPkField() {
 *         return 'field1';
 *      }
 * }
 * </code>
 *   
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage dataaccess.wpdatex
 */
abstract class WpAbstractV extends SimpleDatexV {

	/**
	 * @var An array containing information about how to join the tables
	 */
	protected $_arrJoin = array();
	
	/**
	 * @var A temp attribute storing all fieldname (as table.field format) and their types
	 */
	private $_fieldtypes = array();
	
	/**
	 * @var A temp attribute storing all fieldnames and their aliases
	 */
	private $_fieldaliases = array();
	
	/**
	 * @var A temp attribute storing the fields part of the select statement where all fields will be 
	 * mapped to their respective aliases
	 */
	private $_selectfields = '';
	
	/**
	 * @var A temp attribute mapping the table names to their indexes in $_arrJoin 
	 */
	private $_tablemap = array();
	
  /**
   * @see SimpleDatexM::_getSelectFields()
   */
	protected function _getSelectFields()
	{
		return $this->_selectfields;
	}
	
  /**
   * @see SimpleDatexM::__constructor()
   */
	public function __construct($connector, $log)
	{
		parent::__construct($connector, $log);
		$this->_selectfields = '';
		$this->_fieldaliases = array();
		$this->_fieldtypes = array();
		$this->_tablemap = array();
		
		//-- Create a tmp array for faster field access --
		for ($i=0; $i < count($this->_arrJoin); $i++) {
			$je = $this->_arrJoin[$i];
			$this->_tablemap[$je['alias']] = $i;
			
			foreach($je['fields'] as $jfe) {
				$this->_selectfields .= $je['alias'].'.'.$jfe['name'].' AS '.$jfe['alias'].', ';
				$this->_fieldaliases[$jfe['alias']] = $jfe['name'];
				$this->_fieldtypes[$jfe['alias']] = $jfe['type'];
			}
  	}
  	
  	$this->_selectfields = $this->_selectfields === '' ? '' : substr($this->_selectfields, 0, -2);
	}

  /**
   * @see SimpleDatexM::getFieldType()
   */
  public function getFieldType($fieldname)
  {
    if (array_key_exists($fieldname, $this->_fieldtypes)) {
  		return $this->_fieldtypes[$fieldname];
  	}
  	return '';
  }

  /**
   * @see SimpleDatexM::getFields()
   */
  public function getFields()
  {
  	return array_keys($this->_fieldtypes);
  }
  
  /**
   * @see SimpleDatexV::getJoinAddCondition()
   */
  public function getJoinAddCondition($alias)
  {
  	$i = $this->_tablemap[$alias];
  	if (array_key_exists('joinaddcondition', $this->_arrJoin[$i])) {
  		return $this->_arrJoin[$i]['joinaddcondition'];
  	}
  	
  	return '';
  }
  
  /**
   * @see SimpleDatexV::getJoinKey()
   */
	public function getJoinKey($alias)
	{
		$i = $this->_tablemap[$alias];
		return $this->_arrJoin[$i]['joinkey'];
	}
	
	/**
   * @see SimpleDatexV::getJoinOperator()
   */
	public function getJoinOperator($alias)
	{
		$i = $this->_tablemap[$alias];
		return $this->_arrJoin[$i]['joinoperator'];
	}
	
	/**
   * @see SimpleDatexV::getJoinTarget()
   */
	public function getJoinTarget($alias)
	{
		$i = $this->_tablemap[$alias];
		return $this->_arrJoin[$i]['jointarget'];
	}
	
	/**
   * @see SimpleDatexV::getJoinTargetKey()
   */
	public function getJoinTargetKey($alias)
	{
		$i = $this->_tablemap[$alias];
		return $this->_arrJoin[$i]['jointargetkey'];
	}
	
	/**
   * @see SimpleDatexV::getJoinType()
   */
	public function getJoinType($alias)
	{
		$i = $this->_tablemap[$alias];
		return $this->_arrJoin[$i]['jointype'];
	}
  
  /**
   * @see SimpleDatexV::getTableName()
   */
  public function getTableName($alias)
  {
    $i = $this->_tablemap[$alias];
    if (array_key_exists('table', $this->_arrJoin[$i])) {
      return $this->_arrJoin[$i]['table'];
    }
    return '';
  }
   
  /**
   * @see SimpleDatexV::getTables()
   */
  public function getTables()
  {
    return array_keys($this->_tablemap);
  }
   
  /**
   * @see SimpleDatexM::isView()
   */
  public function isView()
  {
    return true;
  }
}

/* End of file WpAbstractV.php */
/* Location: ./wildpack/dataaccess/wpdatex/WpAbstractV.php */  
